package com.yh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.StrutsUtil;

import com.alibaba.fastjson.JSON;
import com.yh.dao.MovieDao;
import com.yh.dao.impl.MovieDaoImpl;
import com.yh.entity.Movie;

public class MovieServlet {

private Movie movie;

	public Movie getMovie() {
	return movie;
}
public void setMovie(Movie movie) {
	this.movie = movie;
}
	public String getAll(){//得到所有电影信息
		
		MovieDao md=new MovieDaoImpl();
		List<Movie> list=md.getAll();
		ServletActionContext.getRequest().setAttribute("list", list);
		return "success";
	}
	
	
	public void  del() throws IOException{//根据返回的id删除电影信息
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		String id=ServletActionContext.getRequest().getParameter("id");
		System.out.println("id="+id);
		MovieDao md=new MovieDaoImpl();
		
		int i=md.del(Integer.parseInt(id));

		if(i>0){
			out.println(JSON.toJSONString("{'rscode':'1','rsMsg':'删除成功!'}"));
		}else{
			out.println(JSON.toJSONString("{'rscode':'-1','rsMsg':'删除失败!'}"));
		}
	out.flush();
	out.close();
	}
	
	public String add(){//增加电影
		try {
			ServletActionContext.getRequest().setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String name=ServletActionContext.getRequest().getParameter("name");
		String actor=ServletActionContext.getRequest().getParameter("actor");
		String dir=ServletActionContext.getRequest().getParameter("dir");
		String price=ServletActionContext.getRequest().getParameter("price");
	
		Movie mo=new Movie();
		mo.setActor(actor);
		mo.setDir(dir);
		mo.setName(name);
		mo.setPrice(Double.parseDouble(price));
		MovieDao md=new MovieDaoImpl();
		int i=md.add(mo);
		if(i>0){
			try {
				ServletActionContext.getResponse().getWriter().println("<script>alert('添加成功!')</script>");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			try {
				ServletActionContext.getResponse().getWriter().println("<script>alert('添加失败!')</script>");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "success";
	}
	public String  getById(){//通过id查找电影信息并转发到修改页面
		
		String id=ServletActionContext.getRequest().getParameter("id");
		MovieDao md=new MovieDaoImpl();
		Movie mo=new Movie();
		
		mo=md.getById(Integer.parseInt(id));
		
		ServletActionContext.getRequest().setAttribute("mo", mo);
		return "success";

	}
	public String update() throws Exception{//修改页面信息
		
			ServletActionContext.getRequest().setCharacterEncoding("utf-8");
			
			String id=ServletActionContext.getRequest().getParameter("id");
		String name=ServletActionContext.getRequest().getParameter("name");
		String actor=ServletActionContext.getRequest().getParameter("actor");
		String dir=ServletActionContext.getRequest().getParameter("dir");
		String price=ServletActionContext.getRequest().getParameter("price");
	
		Movie mo=new Movie();
		mo.setId(Integer.parseInt(id));
		mo.setActor(actor);
		mo.setDir(dir);
		mo.setName(name);
		mo.setPrice(Double.parseDouble(price));
		MovieDao md=new MovieDaoImpl();
		int i=md.update(mo);
		System.out.println("i="+i);
		if(i>0){
				ServletActionContext.getResponse().getWriter().println("<script>alert('添加成功!')</script>");
		
		}else{	
				ServletActionContext.getResponse().getWriter().println("<script>alert('添加失败!')</script>");	
		}
		return "success";
	}
}
